/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.*;

/**
 *
 * @author Mickey
 */
@WebServlet(name = "FarmerHomeServlet", urlPatterns = {"/FarmerHomeServlet"})
public class FarmerHomeServlet extends HttpServlet {
    
    HttpSession session;
    @EJB PreviousPageBean previousPage;
    @EJB ListOfSeedsBean listOfSeeds;
    @EJB ListOfPostingsBean listOfPostings;

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /*
             * TODO output your page here. You may use following sample code.
             */
            session = request.getSession(false);
            if (session != null) {
                FarmerBean farmer = (FarmerBean)session.getAttribute("user");
                out.println("<html>");
                out.println("<head>");
                out.println("<title>FarmerHomeServlet</title>");            
                out.println("</head>");
                out.println("<body>");
                out.println("<h1>Welcome, Farmer " + farmer.getName() +"</h1>");
                out.println("<form action=\"NavigateToSearchPostingsPageServlet\" method=\"post\">");
                out.println("<input type=\"submit\" value=\"Search Other Farmer's Postings\"/>");
                out.println("</form>");
                out.println("<h2>Your Postings</h2>");
                out.println("<ul>");
                for (PostingBean posting: listOfPostings) {
                    if (farmer.equals(posting.getFarmer())) {
                        out.println("<li>" + posting.getDescription() + "</li>");
                    }
                }
                out.println("</ul>");
                out.println("<form action=\"NavigateToNewPostingPageServlet\" method=\"post\">");
                out.println("<input type=\"submit\" value=\"Create New Posting\"/>");
                out.println("</form>");
                out.println("<br />");
                out.println("<h2>Your Seeds</h2>");
                out.println("<ul>");
                for (SeedBean seed: listOfSeeds) {
                    if (farmer.equals(seed.getFarmer())) {
                        out.println("<li>" + seed.getCommonName() + "</li>");
                    }
                }
                out.println("</ul>");
                out.println("<form action=\"NavigateToNewSeedPageServlet\" method=\"post\">");
                out.println("<input type=\"submit\" value=\"Create New Seed\"/>");
                out.println("</form>");
                out.println("<br />");
                out.println("<form action=\"LogoutServlet\" method=\"post\">");
                out.println("<input type=\"submit\" value=\"Logout\"/>");
                out.println("</form>");
                out.println("</body>");
                out.println("</html>");
            }
            else {
                previousPage.setNext("login.jsp");
                response.sendRedirect(previousPage.getNext());
            }
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
